package com.android.categories;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;

import com.android.R;
import com.android.adapters.MyExpandableListAdapter;
import com.android.data.ServerManager;
import com.android.entities.Group;

public class CategoryActivity extends ExpandableListActivity {

	protected ProgressDialog m_ProgressDialog = null;
	protected Context mContext;
	protected String categoryName;	
	protected MyExpandableListAdapter mAdapter;	 
	ArrayList<Group> listData = new ArrayList<Group>();	
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_list);

        mContext = CategoryActivity.this;
        categoryName = "";
        
        mAdapter = new MyExpandableListAdapter(this, listData);
        setListAdapter(mAdapter);
        
        registerForContextMenu(getExpandableListView());
    }
        
    public void retrieveData() {
    	try {
    		listData = ServerManager.getCategoryGroups(CategoryActivity.this, categoryName);
    		
    	} catch (Exception e) {
    		if (e.getMessage() != null)
    			android.util.Log.e("BACKGROUND_PROC", e.getMessage());
            e.printStackTrace();
    	}
    	runOnUiThread(returnRes);
    }
    
    private Runnable returnRes = new Runnable() {
        public void run() {
        	if (listData != null && listData.size() > 0) {
        		
        		for (int i = 0; i < listData.size(); i++)
        			mAdapter.insertItem(listData.get(i), i);
        	}
            m_ProgressDialog.dismiss();
        }
    };
    
    protected void setCategory(String cName) {
    	this.categoryName = cName;
    }

    /**
     * Context menu
     * **/
    /*@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Sample menu");
        menu.add(0, 0, 0, R.string.expandable_list_sample_action);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();

        String title = ((TextView) info.targetView).getText().toString();

        int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
            Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
                    Toast.LENGTH_SHORT).show();
            return true;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }*/

}
